package com.example.eurekaconsumer.controller;

import com.example.eurekaconsumer.entity.SysUser;
import com.example.eurekaconsumer.service.SysUserService;
import com.example.eurekaconsumer.util.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2021/2/19 10:57
 * @Author: yulei
 * @Description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/greet")
    public String sayHello(@RequestParam String name) {
        return restTemplate.getForObject("http://EUREKA-PRODUCER/sayHello?name=" + name, String.class);
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public SysUser findAll(@RequestParam String username) {
        return sysUserService.findAll(username);
    }

    @RequestMapping("/login")
    public String login(Model model, HttpServletRequest request) {
        model.addAttribute("title", "登录");
        //清空session
        request.getSession().invalidate();
        return "login";
    }

    @PostMapping("/doLogin")
    public String doLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest request) {
        SysUser userInfo = sysUserService.doLogin(username);
        String urls = "redirect:/Index/index";
        if (userInfo != null) {
            request.getSession().setAttribute("userInfo", userInfo);
        } else {
            urls = "redirect:/user/login";
        }
        return urls;
    }

    @RequestMapping("/index")
    public String index(Model model) {
        //首页猜你喜欢商品
        List list = sysUserService.getGoods();
        model.addAttribute("title", "首页");
        model.addAttribute("goods", list);
        return "index";
    }

    @GetMapping("/pro_info/{goodsId}")
    public String pro_info(@PathVariable("goodsId") String goodsId, Model model) {
        //首页猜你喜欢商品
        Map proInfo = sysUserService.proInfo(goodsId);
        model.addAttribute("title", "详情");
        model.addAttribute("proInfo", proInfo);
        return "pro_info";
    }

    @RequestMapping("/pro_list/{keywords}")
    public String pro_list(HttpServletRequest request, @PathVariable("keywords") String keywords, Model model) {
        //商品列表
        List list = sysUserService.getList(request, keywords);
        model.addAttribute("keywords", (request.getParameter("keywords")) == null ? "" : request.getParameter("keywords"));
        model.addAttribute("title", "列表");
        model.addAttribute("goods", list);
        return "pro_list";
    }

    @RequestMapping("/mine")
    public String mine(Model model, HttpServletRequest request) {
        //获取用户信息
        SysUser userInfo = (SysUser) request.getSession().getAttribute("userInfo");
        model.addAttribute("title", "我的");
        model.addAttribute("userInfo", userInfo);
        return "mine";
    }

}
